package com.lestar.www.wuliancamtest.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.lestar.www.wuliancamtest.R;
import com.lestar.www.wuliancamtest.WulianCam.UserInfo;
import com.lestar.www.wuliancamtest.WulianCam.WulianCamManager;

import rx.Subscriber;
import rx.functions.Action1;

/**
 * User: EEE
 * Date: 2016/8/2
 * Time: 10:19
 */
public class LoginPageFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "LoginPageFragment";
    private int mPage;
    private EditText gatewayIdEt;
    private EditText gatewayPwdEt;
    private EditText webUserNameEt;
    private EditText webPwdEt;

    private WulianCamManager mWulianCamManager;

    public static LoginPageFragment newInstance(int page) {

        Bundle args = new Bundle();
        args.putInt("PAGE", page);
        LoginPageFragment fragment = new LoginPageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt("PAGE");
        mWulianCamManager = WulianCamManager.getInstance(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        Log.d(TAG, "onCreateView: " + mPage);
        View root = null;
        if (mPage == 1) {
            root = inflater.inflate(R.layout.web_login_page, container, false);
            webUserNameEt = (EditText) root.findViewById(R.id.web_username_et);
            webPwdEt = (EditText) root.findViewById(R.id.web_pwd_et);
            root.findViewById(R.id.web_login_btn).setOnClickListener(this);
        } else if (mPage == 2) {
            root = inflater.inflate(R.layout.gateway_login_page, container, false);
            gatewayIdEt = (EditText) root.findViewById(R.id.gateway_id_et);
            gatewayPwdEt = (EditText) root.findViewById(R.id.gateway_pwd_et);
            root.findViewById(R.id.gateway_login_btn).setOnClickListener(this);
        }
        return root;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gateway_login_btn:
                final String gatewayId = gatewayIdEt.getText().toString().trim();
                final String gatewayPwd = gatewayPwdEt.getText().toString().trim();
                mWulianCamManager.gatewayLogin(gatewayId, gatewayPwd)
                        .subscribe(new Subscriber<UserInfo>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onNext(UserInfo userInfo) {
                                Intent intent = new Intent(getActivity(), DeviceListActivity.class);
                                startActivity(intent);
                            }
                        });
                break;
            case R.id.web_login_btn:
                final String webUserName = webUserNameEt.getText().toString().trim();
                final String webPwd = webPwdEt.getText().toString().trim();
                mWulianCamManager.webLogin(webUserName, webPwd)
                        .subscribe(new Subscriber<UserInfo>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onNext(UserInfo userInfo) {
                                Intent intent = new Intent(getActivity(), DeviceListActivity.class);
                                startActivity(intent);
                            }
                        });
                break;
            default:
                break;
        }
    }
}
